package pdm.traj.update;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import pdm.traj.entity.TrackPoint2;
import pdm.traj.service.KeyService;
import redis.clients.jedis.Jedis;

public class UpdateTrajSync {
	public static final String TAG = "UpdateTrajSync";
	private Log log = LogFactory.getLog(UpdateTrajSync.class);
	private int index;
	private String ip;
	private int port;
//	private Jedis cli;
	private KeyService keyService;
	public static final int MAX_BUF_SIZE = 20000; 
	public static final int MAX_THREAD_COUNT = 50; 
	public static final int SLEEP_TIME = 5; 
	
	private List<TrackPoint2> buffer = new ArrayList<TrackPoint2>();
	
	private int tCount;// update thread count

	public UpdateTrajSync(int index, String ip, int port) {
		this.index = index;
		this.ip = ip;
		this.port = port;
		tCount = 0;
		log.info("init ip:" + ip + "port:" + port + "MAX_BUF_SIZE:" + MAX_BUF_SIZE + 
				"MAX_THREAD_COUNT:" + MAX_THREAD_COUNT + 
				"SLEEP_TIME:" + SLEEP_TIME);
//		init();
	}

//	public void init() {
//		cli = new Jedis(ip, port);
//	}

	class UpdateTrajThread extends Thread {
		private List<TrackPoint2> data;
		public void run() {
			boolean success = false;
			Jedis cli = new Jedis(ip, port);
			if (data != null) {
				cli.connect();
				for (TrackPoint2 tp : data) {
					String key = keyService.getKeyByTP(tp);
					try {
							cli.append(key, tp.getEntryValue() + ";");
//						if (cli.exists(key)) {
//							cli.append(key, tp.getEntryValue() + ";");
//						} else {
//							cli.set(key, tp.getEntryValue() + ";");
//						}
						success = true;
					} catch (Exception e) {
//						System.err.println("update fail:");
						e.printStackTrace();
					}	
				}
				cli.disconnect();
				data.clear();
				data = null;
			} else {
				System.out.println("UpdateTrajThread data null value");
			}
			tCount--;
//			log.info("index:" + index + "tCount:" + tCount);
		}
		public void setData(List<TrackPoint2> data) {
			this.data = data;
		}
		
	}
	
	public int update(TrackPoint2 tp) {
//		System.out.println(TAG + "update:" + tp.toString());
		if (buffer.size() < MAX_BUF_SIZE) {
			buffer.add(tp);
		} else {
//			List<TrackPoint2> buf = buffer;
			while (tCount >= MAX_THREAD_COUNT) {
				try {
					Thread.sleep(SLEEP_TIME);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			UpdateTrajThread utt = new UpdateTrajThread();
			utt.setData(buffer);
			buffer = new ArrayList<TrackPoint2>();
			tCount++;
//			log.info("index:" + index + "tCount:" + tCount);
			utt.start();			
		}
		return 1;
	}
//	public int update(TrackPoint2 tp) {
////		System.out.println(TAG + "update:" + tp.toString());
//		boolean success = false;
//		String key = keyService.getKeyByTP(tp);
////		while (!success) {
//		try {
//			if (!cli.isConnected()) {
//				cli.connect();
//			}
//			if (cli.exists(key)) {
//				cli.append(key, tp.getEntryValue() + ";");
//			} else {
//				cli.set(key, tp.getEntryValue() + ";");
//			}
////				cli.disconnect();
//			success = true;
//		} catch (Exception e) {
////				System.err.println("update fail: tp:" + tp);
//			
//		}
////		}
//		return 1;
//	}

	public String getIp() {
		return ip;
	}
	public void setIp(String ip) {
		this.ip = ip;
	}
	public int getPort() {
		return port;
	}
	public void setPort(int port) {
		this.port = port;
	}
	public void setKeyService(KeyService keyService) {
		this.keyService = keyService;
	}

}
